package services.frame;

import com.ani.mydragon.dto.template.FormObjTemplate;
import com.ani.mydragon.dto.template.PageTemplate;
import controllers.routes;
import models.application.AniApp;
import models.user.AniUser;
import services.AniPublicPropService;
import services.application.AniAppService;

import java.util.ArrayList;
import java.util.List;

/**
 * User: yeh
 * Date: 3/18/13
 * Time: 5:18 PM
 * For:  wilddragon
 */
public class AniApiFrameService extends AniFrameService {
    public static PageTemplate apiRegForm(AniUser oneUser, Long hostAppId){
        AniApp oneApp = AniAppService.getApp(new AniApp(hostAppId));
        AniAppService.checkAppOwner(oneUser, oneApp);
        PageTemplate appRegPage = new PageTemplate(
                "REGISTER_API",
                "API_REGISTER_PAGE",
                "API_REG_FORM",
                PageTemplate.PageType.form.name(),
                routes.AniApiController.registerApi().url(),
                "SUBMIT_API",
                //redirect
                routes.BaseController.index().url(),
                null
        );
        List<FormObjTemplate> formObjs = new ArrayList<FormObjTemplate>();
        formObjs.add(
                new FormObjTemplate(
                        null,
                        "hostAppId",
                        FormObjTemplate.FormFieldMark.input.name(),
                        FormObjTemplate.FormInputProperties.hidden.name(),
                        String.valueOf(oneApp.id),
                        null,
                        null,
                        null,
                        null
                )
        );
        formObjs.add(
                new FormObjTemplate(
                        "HOST_APP_NAME",
                        "hostAppName",
                        FormObjTemplate.FormFieldMark.span.name(),
                        null,
                        oneApp.appName,
                        null,
                        null,
                        null,
                        null
                )
        );
        formObjs.add(
                new FormObjTemplate(
                        "API_NAME",
                        "apiName",
                        FormObjTemplate.FormFieldMark.input.name(),
                        FormObjTemplate.FormInputProperties.text.name(),
                        null,
                        "API_NAME",
                        null,
                        null,
                        null
                )
        );
        formObjs.add(
                new FormObjTemplate(
                        "HTTP_METHOD",
                        "methodId",
                        FormObjTemplate.FormFieldMark.select.name(),
                        null,
                        null,
                        null,
                        null,
                        AniPublicPropService.getHttpMethodsName(),
                        null
                )
        );
        formObjs.add(
                new FormObjTemplate(
                        "API_TYPE",
                        "typeId",
                        FormObjTemplate.FormFieldMark.select.name(),
                        null,
                        null,
                        null,
                        null,
                        AniPublicPropService.getObjectTypesName(),
                        null
                )
        );
        formObjs.add(
                new FormObjTemplate(
                        "URL",
                        "url",
                        FormObjTemplate.FormFieldMark.input.name(),
                        FormObjTemplate.FormInputProperties.text.name(),
                        null,
                        "URL",
                        null,
                        null,
                        null
                )
        );
        formObjs.add(
                new FormObjTemplate(
                        "DESCRIPTION",
                        "description",
                        FormObjTemplate.FormFieldMark.textarea.name(),
                        null,
                        null,
                        "DESCRIPTION",
                        null,
                        null,
                        null
                )
        );
        formObjs.add(generateApiParamsSubForm());
        appRegPage.objects = formObjs;
        return appRegPage;
    }
    protected static FormObjTemplate generateApiParamsSubForm(){
        List<FormObjTemplate> paramFormObjs = new ArrayList<FormObjTemplate>();
        paramFormObjs.add(
                new FormObjTemplate(
                        "PARAM_NAME",
                        "paramName",
                        FormObjTemplate.FormFieldMark.input.name(),
                        FormObjTemplate.FormInputProperties.text.name(),
                        null,
                        "PARAM_NAME",
                        null,
                        null,
                        null
                )
        );
        paramFormObjs.add(
                new FormObjTemplate(
                        "PARAM_DEFAULT_VALUE",
                        "paramValue",
                        FormObjTemplate.FormFieldMark.input.name(),
                        FormObjTemplate.FormInputProperties.text.name(),
                        null,
                        "PARAM_VALUE",
                        null,
                        null,
                        null
                )
        );
        paramFormObjs.add(
                new FormObjTemplate(
                        "IS_REQUIRED",
                        "isRequired",
                        FormObjTemplate.FormFieldMark.input.name(),
                        FormObjTemplate.FormInputProperties.checkbox.name(),
                        null,
                        null,
                        null,
                        null,
                        null
                )
        );
        paramFormObjs.add(
                new FormObjTemplate(
                        "DESCRIPTION",
                        "description",
                        FormObjTemplate.FormFieldMark.input.name(),
                        FormObjTemplate.FormInputProperties.text.name(),
                        null,
                        null,
                        null,
                        null,
                        null
                )
        );
        //    public String description;
        //return entire sub form
        return new FormObjTemplate(
                "PARAMETERS",
                "params",
                FormObjTemplate.FormFieldMark.form.name(),
                null,
                null,
                null,
                null,
                null,
                paramFormObjs
        );
    }
}
